
package com.ifcar.app.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;

import com.ifcar.app.activity.R;

public class MenuPopup extends MyPopupWindow {

    private OnClickListener mOnClickListener;

    private Activity mActivity;

    private MenuCallback mMenuCallback;

    public MenuPopup(Activity activity) {
        super(activity, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        mActivity = activity;
        setAnimationStyle(R.style.menuPopupAnimation);

    }
    
    public void setMenuCallback(MenuCallback callback) {
        mMenuCallback = callback;
    }

    public void setOnClickListener(OnClickListener listener, int[] clickIds) {
        mOnClickListener = listener;
        View contentView = getContentView();
        if (contentView != null) {
            if (clickIds != null && clickIds.length > 0) {
                for (int id : clickIds) {
                    View v = contentView.findViewById(id);
                    v.setOnClickListener(mOnClickListener);
                }
            }
        }
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        final int action = event.getAction();
        
        if (isShowing()) {
            if (checkDismissKey(keyCode)) {
                if (action == KeyEvent.ACTION_UP) {
                    callMenuCallbackClose();
                    dismiss();
                }
                return true;
            }
            return false;
        }

        if (checkShowKey(keyCode)) {
            if (action == KeyEvent.ACTION_UP && callMenuCallbackOpen()) {
                mActivity.findViewById(android.R.id.content).post(new Runnable() {
                    public void run() {
                        showAtLocation(mActivity.findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
                    }
                });
            }
            return true;
        }
        return false;
    }

    protected boolean checkDismissKey(int keyCode) {
        return keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU;
    }

    protected boolean checkShowKey(int keyCode) {
        return keyCode == KeyEvent.KEYCODE_MENU;
    }

    private boolean callMenuCallbackOpen() {
        if (mMenuCallback == null) {
            return true;
        }
        return mMenuCallback.onMenuOpen();
    }
    
    private void callMenuCallbackClose() {
        if (mMenuCallback != null) {
            mMenuCallback.onMenuClose();
        }
    }

    public interface MenuCallback {
        /**
         * 菜单显示时调用,返回false不显示菜单
         * 
         * @return
         */
        public boolean onMenuOpen();

        /**
         * 菜单隐藏时调用
         * 
         * @return
         */
        public void onMenuClose();
    }
}
